﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PenguinForce.Collisions;
using Microsoft.Xna.Framework.Content;

namespace PenguinForce
{
    public class HUDRendererComponent : DrawableGameComponent, IAbilityController
    {
        #region Constants

        /// <summary>
        /// Setting this value above 0 makes the game frustrating.  This must be avoided above all else.
        /// </summary>
        private const float FISH_COOLDOWN = 0000.0f;

        private const float SAND_COOLDOWN = 1000.0f;
        private const float SLIPPERY_COOLDOWN = 2000.0f;
        private const float SIREN_COOLDOWN = 5000.0f;

        #endregion

        #region Variables

        private SpriteBatch _spriteBatch;
        private Texture2D _blankTexture;
        private SpriteFont _font;

        private double _timeLimit;
        private double _timeLeft;

        private Rectangle _titleSafeArea;

        private bool _isFishTriggered;
        private bool _isSandTriggered;
        private bool _isSlipperyTriggered;
        private bool _isSirenTriggered;

        private float _fishDuration;
        private float _sandDuration;
        private float _slipperyDuration;
        private float _sirenDuration;

        private float _sandAlpha;
        private float _slipperyAlpha;
        private float _sirenAlpha;

        private Texture2D _sandbagTexture;
        private Texture2D _iceSlickTexture;
        private Texture2D _sirenTexture;

        #endregion

        #region Constructors

        public HUDRendererComponent(Microsoft.Xna.Framework.Game game, double timeLimit)
            : base(game)
        {
            _timeLeft = timeLimit;
            _timeLimit = timeLimit;

            _isFishTriggered = false;
            _isSandTriggered = false;
            _isSlipperyTriggered = false;
            _isSirenTriggered = false;

            _sandAlpha = 1;
            _slipperyAlpha = 1;
            _sirenAlpha = 1;
        }

        #endregion

        #region Properties

        public bool IsFishTriggered
        {
            get
            {
                return _isFishTriggered;
            }
        }

        public bool IsSandTriggered
        {
            get
            {
                return _isSandTriggered;
            }
        }

        public bool IsSlipperyTriggered
        {
            get
            {
                return _isSlipperyTriggered;
            }
        }

        public bool IsSirenTriggered
        {
            get
            {
                return _isSirenTriggered;
            }
        }

        #endregion

        #region Methods

        public void TriggerFish()
        {
            if (_fishDuration <= 0)
            {
                _fishDuration = FISH_COOLDOWN;
                _isFishTriggered = true;
            }
        }

        public void TriggerSand()
        {
            if (_sandDuration <= 0)
            {
                _sandDuration = SAND_COOLDOWN;
                _sandAlpha = 0;
                _isSandTriggered = true;
            }

        }

        public void TriggerSlippery()
        {
            if (_slipperyDuration <= 0)
            {
                _slipperyDuration = SLIPPERY_COOLDOWN;
                _slipperyAlpha = 0;
                _isSlipperyTriggered = true;
            }

        }

        public void TriggerSiren()
        {
            if (_sirenDuration <= 0)
            {
                _sirenDuration = SIREN_COOLDOWN;
                _sirenAlpha = 0;
                _isSirenTriggered = true;
            }
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            ContentManager content = base.Game.Content;

            _spriteBatch = new SpriteBatch(base.Game.GraphicsDevice);
            _blankTexture = content.Load<Texture2D>("blank");
            _font = content.Load<SpriteFont>("gamefont");
            _titleSafeArea = Game.GraphicsDevice.Viewport.TitleSafeArea;

            _iceSlickTexture = content.Load<Texture2D>("bucket");
            _sandbagTexture = content.Load<Texture2D>("sandbag");
            _sirenTexture = content.Load<Texture2D>("megaphone");
        }

        public override void Update(GameTime gameTime)
        {
            UpdateTimer(gameTime);
            UpdateAbilities(gameTime);
            base.Update(gameTime);
        }

        private void UpdateTimer(GameTime gameTime)
        {
            _timeLeft -= gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0;
        }

        private void UpdateAbilities(GameTime gameTime)
        {
            float elapsedTime = gameTime.ElapsedGameTime.Milliseconds;

            _fishDuration -= elapsedTime;
            if (_fishDuration <= 0)
            {
                _isFishTriggered = false;
            }
            _sandDuration -= elapsedTime;
            _sandAlpha = (SAND_COOLDOWN - _sandDuration) / SAND_COOLDOWN;
            if (_sandDuration <= 0)
            {
                _isSandTriggered = false;

            }
            _slipperyDuration -= elapsedTime;
            _slipperyAlpha = (SLIPPERY_COOLDOWN - _slipperyDuration) / SLIPPERY_COOLDOWN;
            if (_slipperyDuration <= 0)
            {
                _isSlipperyTriggered = false;

            }
            _sirenDuration -= elapsedTime;
            _sirenAlpha = (SIREN_COOLDOWN - _sirenDuration) / SIREN_COOLDOWN;
            if (_sirenDuration <= 0)
            {
                _isSirenTriggered = false;
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            _spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            // Draw level status:
            _spriteBatch.Draw(_blankTexture, new Rectangle(_titleSafeArea.Left + 20, _titleSafeArea.Top + 20, 400, 10 + _font.LineSpacing * 2), new Color(0.0f, 0.0f, 0.0f, 0.5f));
            _spriteBatch.DrawString(_font, string.Format("Penguins: {0}", CollisionManager.Instance.Entities.Count), new Vector2(_titleSafeArea.Left + 28, _titleSafeArea.Top + 24), Color.White);
            _spriteBatch.DrawString(_font, string.Format("Par for the course: {0}", CollisionManager.Instance.GetCurrentLevel().m_parPenguins), new Vector2(_titleSafeArea.Left + 28, _titleSafeArea.Top + 24 + _font.LineSpacing), Color.White);

            // Draw timer:
            _spriteBatch.Draw(_blankTexture, new Rectangle(_titleSafeArea.Right - 88, _titleSafeArea.Top + 18, 52, 52), new Color(0.0f, 0.0f, 0.0f, 0.5f));
            _spriteBatch.DrawString(_font, string.Format("{0}:{1:00}", (int)(_timeLeft / 60), (int)(_timeLeft % 60)), new Vector2(_titleSafeArea.Right - 88, _titleSafeArea.Top + 28), Color.Yellow);

            // Show cool down meter
            Rectangle dest = new Rectangle(_titleSafeArea.Right - 244, _titleSafeArea.Top + 20, 50, 48);
            _spriteBatch.Draw(_blankTexture, new Rectangle(dest.X - 2, dest.Y - 2, 154 + 4, 48 + 4), new Color(0.0f, 0.0f, 0.0f, 0.5f));

            // Draw sand cool down icon
            _spriteBatch.Draw(_sandbagTexture, new Rectangle(dest.X + 96, _titleSafeArea.Top + 20, 50, 48), new Color(255, 255, 255, _sandAlpha));

            // Draw siren cool down icon
            _spriteBatch.Draw(_sirenTexture, new Rectangle(dest.X + 48, _titleSafeArea.Top + 20, 54, 48), new Color(255, 255, 255, _sirenAlpha));

            // Draw slippery cool down icon
            _spriteBatch.Draw(_iceSlickTexture, dest, new Color(255, 255, 255, _slipperyAlpha));

            _spriteBatch.End();
        }

        #endregion
    }
}